package com.mtz.controller;

import com.mtz.VO.QuesAnsVO;
import com.mtz.VO.ResultVO;
import com.mtz.exception.MtzException;
import com.mtz.form.QuesAnsForm;
import com.mtz.service.QuesAnsService;
import com.mtz.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 丁艺伟
 * @date 2018/12/3 0003 上午 11:28
 */
@Api(description = "问答接口")
@RestController
@RequestMapping("/quesAns")
public class QuesAnsController {

    @Autowired
    private QuesAnsService quesAnsService;

    /**
     * @param questionId 问题id
     * @return 问题+答案
     */
    @ApiOperation(value = "查找questionId的问答", notes = "查找问答")
    @GetMapping("/questionId")
    public ResultVO findById(@ApiParam(value = "问题id",required = true)
                                 @RequestParam("questionId") Integer questionId) {
        List<QuesAnsVO> quesAnsVOList = new ArrayList<QuesAnsVO>();

        QuesAnsVO quesAnsVO = quesAnsService.findQuesAnsById(questionId);
        quesAnsVOList.add(quesAnsVO);
        return ResultVOUtil.success(quesAnsVOList);
    }

    /**
     * @return 返回所有问答
     */
    @ApiOperation(value = "查找所有问答", notes = "查找所有问答")
    @GetMapping("/all")
    public ResultVO findAll() {
        List<QuesAnsVO> quesAnsVOList = quesAnsService.findAll();
        return ResultVOUtil.success(quesAnsVOList);
    }

    /**
     * 查找knowledgeType的所有问答
     * @param knowledgeType 知识类型
     * @return
     */
    @ApiOperation(value = "查找knowledgeType的所有问答", notes = "查找对应知识类型的所有问答")
    @GetMapping("/knowledgeType")
    public ResultVO findAllByType(@ApiParam(value = "知识类型",required = true)
                                      @RequestParam("knowledgeType") Integer knowledgeType) {
        List<QuesAnsVO> quesAnsVOList = quesAnsService.findAllByKnowledgeType(knowledgeType);
        return ResultVOUtil.success(quesAnsVOList);
    }

    @ApiOperation(value = "根据关键词查找问答", notes = "根据关键词查找问答")
    @GetMapping("/keyword")
    public ResultVO findAllByKeyword(@ApiParam(value = "关键词",required = true)
                                  @RequestParam("keyword") String keyword) {
        List<QuesAnsVO> quesAnsVOList = quesAnsService.findAllByKeyword(keyword);
        return ResultVOUtil.success(quesAnsVOList);
    }

    /**
     * 添加问答
     * @param form 前端表单提交数据
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "添加问答", notes = "添加问答")
    @PostMapping("/add")
    public ResultVO add(@ApiParam(value = "knowledgeType,question,keyword,answer,author,angle",required = true)
                            @Valid QuesAnsForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new MtzException(-1, bindingResult.getFieldError().getDefaultMessage());
        }

        QuesAnsVO quesAnsVO = quesAnsService.addQuesAns(form);
        return ResultVOUtil.success(quesAnsVO);
    }

    /**
     * 更新问答
     * @param form 前端表单提交数据
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "更新问答", notes = "更新问答")
    @PostMapping("/edit")
    public ResultVO edit(@ApiParam(value = "id,question,keyword,answer,author,angle",required = true)
                             @Valid QuesAnsForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new MtzException(-1, bindingResult.getFieldError().getDefaultMessage());
        }
        quesAnsService.editQuesAns(form);
        return ResultVOUtil.success();
    }

    @ApiOperation(value = "删除问答", notes = "删除问答")
    @PostMapping("/deleteTrue")
    public ResultVO deleteQuesAns(@ApiParam(value = "问题id",required = true)
                                      @RequestParam("questionId") Integer questionId) {
        quesAnsService.deleteQuesAns(questionId);
        return ResultVOUtil.success();
    }
}
